package com.xuecheng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.base.mapper.CourseBaseMapper;
import com.xuecheng.base.mapper.CourseTeacherMapper;
import com.xuecheng.base.model.po.CourseBase;
import com.xuecheng.base.model.po.CourseTeacher;
import com.xuecheng.base.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Override
    public List<CourseTeacher> courseTeacherList(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);

        return courseTeachers;
    }

    @Override
    public CourseTeacher createCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if (!courseBase.getCompanyId().equals(companyId)){
            throw new XueChengPlusException("本机构只能添加本机构的课程教师信息");
        }
        if (StringUtils.isBlank(courseTeacher.getTeacherName())){
            throw new XueChengPlusException("教师名称为空");
        }
        if (StringUtils.isBlank(courseTeacher.getPosition())){
            throw new XueChengPlusException("教师职位为空");
        }
        int insert = courseTeacherMapper.insert(courseTeacher);
        if(insert<=0){
            throw new RuntimeException("新增课程基本信息失败");
        }
        return courseTeacher;
    }

    @Override
    public CourseTeacher getCourseTeacher(Long id) {
        return courseTeacherMapper.selectById(id);
    }

    @Override
    public CourseTeacher updateCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if (!courseBase.getCompanyId().equals(companyId)){
            throw new RuntimeException("本机构只能修改本机构的课程教师信息");
        }
        if (StringUtils.isBlank(courseTeacher.getTeacherName())){
            throw new XueChengPlusException("教师名称为空");
        }
        if (StringUtils.isBlank(courseTeacher.getPosition())){
            throw new XueChengPlusException("教师职位为空");
        }
        int update = courseTeacherMapper.updateById(courseTeacher);
        if(update<=0){
            throw new RuntimeException("修改课程教师信息失败");
        }
        return courseTeacher;
    }

    @Override
    public void deleteCourseTeacher(Long companyId,Long courseId, Long id) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (!courseBase.getCompanyId().equals(companyId)){
            throw new RuntimeException("本机构只能删除本机构的课程教师信息");
        }
        int delete = courseTeacherMapper.deleteById(id);
        if(delete<=0){
            throw new RuntimeException("删除课程教师信息失败");
        }

    }
}
